package com.mz.ozy.activity.find;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.ReportAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.ReportBean;
import com.mz.ozy.mvp.presenter.ReportPresenter;
import com.mz.ozy.mvp.view.ReportContract;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.CommonItemDecoration;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ReportActivity extends BaseActivity<ReportContract.View, ReportPresenter> implements ReportContract.View {
    private TextView mTvFindReport;
    private RecyclerView mRvFindReport;
    private ReportAdapter reportAdapter;
    private TextView mRvReportSubmit;
    private String mUid;
    private String nickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        initTitle();
        tvCenterTitle.setText("举报");
        tvTitle.setVisibility(View.GONE);
        initData();
        initView();
    }

    private void initData() {
        mUid = getIntent().getStringExtra("uid");
        nickname = getIntent().getStringExtra("name");
        if (TextUtils.isEmpty(nickname)) {
            nickname = "";
        }
    }

    private void initView() {
        mTvFindReport = findViewById(R.id.tv_find_report_label);
        mTvFindReport.setText("举报" + nickname + "用户:");
        mRvFindReport = findViewById(R.id.rv_find_report);
        mRvFindReport.setHasFixedSize(true);
        mRvFindReport.setLayoutManager(new GridLayoutManager(this, 2));
        reportAdapter = new ReportAdapter(getTestCourseList3());
        mRvFindReport.setAdapter(reportAdapter);
        int hspace = getResources().getDimensionPixelSize(R.dimen.dp_10);
        int vspace = getResources().getDimensionPixelSize(R.dimen.dp_50);
        mRvFindReport.addItemDecoration(new CommonItemDecoration(0, hspace, vspace, hspace));

        mRvReportSubmit = findViewById(R.id.tv_report_submit);
        mRvReportSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ToastUtils.showToast(reportAdapter.getSelectReason().toString());
                List<String> selectReason = reportAdapter.getSelectReason();
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < selectReason.size(); i++) {
                    String reason = selectReason.get(i);
                    stringBuilder.append(reason);
                    if (i < selectReason.size() - 1)
                        stringBuilder.append(",");
                }

                mPresenter.reportFriend(ReportActivity.this, mUid, stringBuilder.toString());
            }
        });
    }

    List<ReportBean> getTestCourseList3() {
        List<ReportBean> list = new ArrayList<>();
        list.add(new ReportBean("垃圾营销", false));
        list.add(new ReportBean("有害信息", false));
        list.add(new ReportBean("涉黄信息", false));
        list.add(new ReportBean("内容抄袭", false));
        list.add(new ReportBean("不实信息", false));
        list.add(new ReportBean("违法信息", false));
        list.add(new ReportBean("人身攻击", false));
        list.add(new ReportBean("诈骗信息", false));
        return list;
    }

    @Override
    public void reportSuccess(JSONObject jsonObject) {
//        ToastUtils.showToast("举报成功");
        Toast toast = new Toast(this);
        toast.setGravity(Gravity.CENTER, 0, 0);
        final View toastView = View.inflate(this, R.layout.toast_report_view, null);
        toast.setView(toastView);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 2000);//2秒后执
    }
}
